// Lindsay Gilmour
// Adam Lusch
// Dan Niceswanger
//
// Computer Graphics Project 2:
// Teapot racing!

#include "Timer.h"

Timer::Timer(GLfloat x, GLfloat y, bool lap)
{
	scrPosX = x;
	scrPosY = y;
	
	isLap = lap;
	
	totalTime = 0.0;
	lapTimes.clear();
}

Timer::~Timer()
{
}

/* Change view to allow timer to be displayed in screen coordinates */
void Timer::setOrthographicProjection() 
{
	// switch to projection mode
	glMatrixMode(GL_PROJECTION);

	// save previous matrix which contains the 
	//settings for the perspective projection
	glPushMatrix();

	// reset matrix
	glLoadIdentity();

	// set a 2D orthographic projection, width = height = 40
	gluOrtho2D(0.0, 40.0, 0.0, 40.0);

	// mover the origin from the bottom left corner
	// to the upper left corner
	glTranslatef(0.0, 40.0, 0.0);

	glMatrixMode(GL_MODELVIEW);
}

/* Reset view to previous settings to render other objects correctly */
void Timer::resetPerspectiveProjection() 
{
	glMatrixMode(GL_PROJECTION);
	glPopMatrix();
	glMatrixMode(GL_MODELVIEW);
}


// Origin is the top-left corner for time display
void Timer::displayTime()
{
	getText();

	setOrthographicProjection();
	glLoadIdentity();

	glPushMatrix();
		glPushAttrib(GL_ALL_ATTRIB_BITS);
		glLineWidth(1.6);
		glDisable(GL_LIGHTING);
		glColor3f(1.0f, 1.0f, 1.0f);
			
		glTranslatef(scrPosX, -scrPosY, 0.0);
		glScalef(0.0075f, 0.01f, 0.01f);
		
		for (unsigned int i = 0; i < sTime.size(); i++)
		{
			glutStrokeCharacter(GLUT_STROKE_MONO_ROMAN, sTime[i]);
		}
		glPopAttrib();
	glPopMatrix();
	
	if (!isLap && lapTimes.size() > 0)
	{
		glPushMatrix();
			glPushAttrib(GL_ALL_ATTRIB_BITS);
			glLineWidth(1.4);
			glDisable(GL_LIGHTING);
			glColor3f(1.0f, 1.0f, 1.0f);
				
			for (unsigned int i = 0; i < lapTimes.size(); i++)
			{
				glPushMatrix();
					// display individual lap times under total time
					glTranslatef(1.1, -scrPosY - (i+1)*1.5, 0.0);
					glScalef(0.0060f, 0.008f, 0.008f);
					
					
					string lap_time = getText(lapTimes[i]);
					lap_time = lap_time.substr(6, lap_time.size()-6);
					
					stringstream buf;
					buf << "Lap";
					buf << i+1;
					lap_time = buf.str() + " " + lap_time;
					
					for (unsigned int j = 0; j < lap_time.size(); j++)
					{
						glutStrokeCharacter(GLUT_STROKE_MONO_ROMAN, lap_time[j]);
					}
				glPopMatrix();
			}
			glPopAttrib();
		glPopMatrix();
	}

	resetPerspectiveProjection();
}

string Timer::getText(double input)
{	
	double dTime;

	if (input == 0.0)
		dTime = getSeconds();
	else
		dTime = input;
	
	stringstream buf;
	
        // first char should be 0 if minutes are less than 10
	if (dTime/60.0 < 10.0)
		buf << "0";
	buf << (int)dTime/60;
	
	if (isLap)
		sTime = "Lap " + buf.str() + ":";   // minutes
	else
		sTime = "Total " + buf.str() + ":";
	
	buf.str("");
	
        // first char should be 0 if seconds are less than 10
	if ((int)dTime%60 < 10)
		buf << "0";
	buf << (int)dTime%60;
	sTime.append(buf.str());   // seconds
	
	buf.str("");
	
	buf << (float)(dTime - (int)dTime);
	sTime.append(buf.str().substr(1,2)); // deciseconds
	
	/*if (!isLap)
	{
		string tempLap;
		//cout << lapTimes.size() << endl;
		for (int i = 0; i < lapTimes.size(); i++)
		{
			tempLap = "\nLap ";// + (string)i + (string)lapTimes[i];
			
			sTime.append(tempLap);
			cout << sTime << endl;
		}
	}*/
	
	return sTime;
}

double Timer::getSeconds()
{
	gettimeofday(&end, NULL);
	
	double t1 = start.tv_sec + (start.tv_usec/1000000.0);
	double t2 = end.tv_sec + (end.tv_usec/1000000.0);
	
	return t2 - t1;
}

double Timer::getTotal()
{
	return totalTime;
}

vector <double> Timer::getLaps()
{
	return lapTimes;
}

void Timer::startTimer()
{
	gettimeofday(&start, NULL);
}

void Timer::saveTimes(double lap)
{
	lapTimes.push_back(lap);
	totalTime += lap;
}

/*void Timer::setDuration(int dur, double lapTime)
{
	duration = dur;*/
